package cn.edu.neusoft.yyd208.foodorder.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.edu.neusoft.yyd208.foodorder.R;
import cn.edu.neusoft.yyd208.foodorder.bean.RegisterBean;
import cn.edu.neusoft.yyd208.foodorder.listener.TListener;
import cn.edu.neusoft.yyd208.foodorder.model.PurchModel;

public class PurchActivity extends BaseActivity {
    TextView tv_purchuser,tv_foodnamepurch,tv_totalprice;
    EditText et_addresspurch,et_number,et_purchtime;
    Button bt_corder;
    Spinner sp_arrive;
    PurchModel purchModel;
    String user_id,food_id,num,sum,suggesttime;
    TListener<RegisterBean> purchListener=new TListener<RegisterBean>() {
        @Override
        public void onResponse(RegisterBean registerBean) {
            if(registerBean.getSuccess().equals("1")){
                showToast("成功");
            }
            else {
                showToast("失败");
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };
    @Override
    void initView() {
        setLayout_file(R.layout.activity_purch);
        tv_purchuser=(TextView)findViewById(R.id.tv_purchuser);
        tv_foodnamepurch=(TextView)findViewById(R.id.tv_foodnamepurch);
        tv_totalprice=(TextView)findViewById(R.id.tv_totalprice);
        et_addresspurch=(EditText) findViewById(R.id.et_addresspurch);
        et_number=(EditText)findViewById(R.id.et_number);
        et_purchtime=(EditText)findViewById(R.id.et_purchtime);
        bt_corder=(Button) findViewById(R.id.bt_corder);
        sp_arrive=(Spinner) findViewById(R.id.sp_arrive);
        SimpleDateFormat format=new SimpleDateFormat();
        Date curDate=new Date(System.currentTimeMillis());
        et_purchtime.setText(format.format(curDate));
        Intent intent = getIntent();
        String foodname2=intent.getStringExtra("foodname");
        tv_foodnamepurch.setText(foodname2);
       // tv_totalprice.setText(intent.getStringExtra("foodprice"));
    }

    @Override
    void initEvent() {
        et_number.addTextChangedListener(new TextWatcher() {
            int l=0;////////记录字符串被删除字符之前，字符串的长度
            int location=0;//记录光标的位置
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                l=s.length();
                location=et_number.getSelectionStart();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(et_number.getText().toString().equals("")){
                    float single=0;
                }
                else {
                String num=et_number.getText().toString();
                    Intent intent = getIntent();
                    tv_totalprice.setText(intent.getStringExtra("foodprice"));
                String foodprice=tv_totalprice.getText().toString();
                int money=Integer.parseInt(num);
                Float single=Float.parseFloat(foodprice);
                Float result = money * single;
                tv_totalprice = (TextView)findViewById(R.id.tv_totalprice);
                tv_totalprice.setText(""+result);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        bt_corder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(et_number.getText().toString().trim().equals("")){
                    showToast("不能为空");
                }
                else{
                    showToast("购买成功");
                    finish();
                }
            }
        });

    }

    @Override
    void initData() {
        String[] arrivetime = {"10:30-11:00", "11:00-11:30", "11:30-12:00", "12:00-12:30","12:30-13:00"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arrivetime);
        //设置下拉按钮样式
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //将spinner与ArrayAdapter绑定
        sp_arrive.setAdapter(adapter);
        sp_arrive.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);

        String username2=sp.getString("username","");

        tv_purchuser.setText("购买用户："+username2);
        String address2=sp.getString("address","");
        et_addresspurch.setText(address2);

        purchModel=new PurchModel();
        purchModel.getPurchMessage(user_id,food_id,num,sum,suggesttime,purchListener);
    }

}
